package com.yc.hotel.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yc.hotel.dto.OrderInfoDTO;
import com.yc.hotel.dto.RoomInfoDTO;
import com.yc.hotel.enums.ResultEnum;
import com.yc.hotel.room.feign.IRoomInfoFeignClient;
import com.yc.hotel.service.IOrderInfoService;
import com.yc.hotel.vo.ResultVO;

@RestController
@RequestMapping("/order")
public class OrderInfoController {

	@Autowired
	private IOrderInfoService orderInfoService;
	
	@Autowired
	private IRoomInfoFeignClient roomInfoService;
	
	
	@PostMapping("/add")
	public ResultVO add(String rno, String uno, String tno, String ino) {
		
		int result = orderInfoService.add(rno, uno, tno, ino);
		System.out.println("add result: " + result);
		if(result > 0) {
			return new ResultVO(ResultEnum.SUCCESS, result);
		}
		return new ResultVO(ResultEnum.ERROR);
	};
	
	@PostMapping("/outOrder")
	public ResultVO outOrder(String ono, String price) {

		int result = orderInfoService.outOrder(ono, price);
		return new ResultVO(ResultEnum.SUCCESS, result);
		
	};
	
	@PostMapping("/closeOrder")
	public ResultVO closeOrder(String ono, String rno) {

		int result = orderInfoService.update(ono, 0);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rno", rno);
		map.put("status", 1);
		result += roomInfoService.updateStatus(map);
		if(result > 1) {
			return new ResultVO(ResultEnum.SUCCESS, result);
		}
		
		return new ResultVO(ResultEnum.ERROR);
	};
	
	@PostMapping("/check")//商家确认订单
	public ResultVO check(String rno, String tno, String status) {
		if(status.contentEquals("2")) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("rno", rno);
			map.put("status", status);
			int result = orderInfoService.update(map);				
			
			if(tno.equals("101")) {
				map.put("status", 0);
			}else {
				map.put("status", 3);
			}
			result += roomInfoService.updateStatus(map);
			if(result > 1) {
				return new ResultVO(ResultEnum.SUCCESS, result);
			}
		}else if(status.contentEquals("1")){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("rno", rno);
			map.put("status", 0);
			int result = orderInfoService.update(map);				
			
			
			map.put("status", 1);
			
			result += roomInfoService.updateStatus(map);
			if(result > 1) {
				return new ResultVO(ResultEnum.SUCCESS, result);
			}
		}
		
		return new ResultVO(ResultEnum.ERROR);
	};
	
	@PostMapping("/finds")
	public ResultVO finds(@RequestParam Map<String, Object> map) {
		List<OrderInfoDTO> orders = orderInfoService.findRoom(map);
		for(OrderInfoDTO order : orders) {
			Integer rno = order.getRno();
			RoomInfoDTO room = roomInfoService.findByRno(rno);
			order.setPics(room.getPics());
			order.setRname(room.getRname());
			order.setPrice(room.getPrice());
			order.setTno(room.getTno());
		}
		return new ResultVO(ResultEnum.SUCCESS, orders);

	};
}
